package com.towexin.application.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.towexin.application.activiti.service.ActivitiesService;
import com.towexin.application.activiti.service.ProcessFormService;
import com.towexin.application.system.domain.User;
import com.towexin.application.system.service.SystemSettingService;
import com.towexin.application.system.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * TODO
 *
 * @author 唐伟鑫
 * @version 1.0
 * @date 2021/4/8 18:46
 */
@RestController
public class PageController {

	@Autowired
	private UserService userService;

	@Autowired
	private SystemSettingService systemSettingService;

	@Autowired
	private ProcessFormService processFormService;

	@Autowired
	private ActivitiesService activitiesService;

	@ApiOperation("登录页面")
	@GetMapping("login")
	public ModelAndView loginPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("login");
		return modelAndView;
	}

	@ApiOperation("后台管理")
	@GetMapping("index")
	public ModelAndView indexPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		modelAndView.addObject("system", systemSettingService.getById("system-setting"));
		modelAndView.addObject("user", userService
				.getOne(new QueryWrapper<User>().eq("username", SecurityUtils.getSubject().getPrincipal().toString())));
		return modelAndView;
	}

	@ApiOperation("后台首页")
	@RequiresPermissions("system.welcome")
	@GetMapping("page/welcome")
	public ModelAndView pageWelcom() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/welcome");
		return modelAndView;
	}

	@ApiOperation("用户列表页")
	@RequiresPermissions("system.user")
	@GetMapping("page/system/user")
	public ModelAndView userPage() {
		Subject subject = SecurityUtils.getSubject();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/user");
		modelAndView.addObject("userIds", userService.noUserIds());
		modelAndView.addObject("userName", subject.getPrincipal());
		return modelAndView;
	}

	@ApiOperation("角色列表页")
	@RequiresPermissions("system.role")
	@GetMapping("page/system/role")
	public ModelAndView rolePage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/role");
		return modelAndView;
	}

	@ApiOperation("菜单列表页")
	@RequiresPermissions("system.menu")
	@GetMapping("page/system/menu")
	public ModelAndView menuPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/menu");
		return modelAndView;
	}

	@ApiOperation("权限列表页")
	@RequiresPermissions("system.authority")
	@GetMapping("page/system/authority")
	public ModelAndView authorityPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/authority");
		return modelAndView;
	}

	@ApiOperation("日志列表页")
	@RequiresPermissions("system.log")
	@GetMapping("page/system/log")
	public ModelAndView logPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/log");
		return modelAndView;
	}

	@ApiOperation("系统设置页")
	@RequiresPermissions("system.setting")
	@GetMapping("page/system/setting")
	public ModelAndView systemSettingPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/setting");
		return modelAndView;
	}

	@ApiOperation("文件管理列表页")
	@RequiresPermissions("system.files")
	@GetMapping("page/system/file-list")
	public ModelAndView fileListPage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/system/file-list");
		return modelAndView;
	}

	@ApiOperation("page目录下通用页面")
	@GetMapping("page/{page}")
	public ModelAndView pageRoot(@PathVariable String page) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/" + page);
		return modelAndView;
	}

	@ApiOperation("page目录下一级目录通用页面")
	@GetMapping("page/{levelOne}/{page}")
	public ModelAndView pageLevelOne(@PathVariable String levelOne, @PathVariable String page) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/" + levelOne + "/" + page);
		return modelAndView;
	}

	@ApiOperation("page目录下二级目录通用页面")
	@GetMapping("page/{levelOne}/{levelTwo}/{page}")
	public ModelAndView pageLevelTwo(@PathVariable String levelOne, @PathVariable String levelTwo,
			@PathVariable String page) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/" + levelOne + "/" + levelTwo + "/" + page);
		return modelAndView;
	}

	@GetMapping("/page/business/model/{id}")
	public ModelAndView modelDesign(@PathVariable String id){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("processDefinitionId", id);
		modelAndView.setViewName("page/business/model-design");
		return modelAndView;
	}

	@GetMapping("/page/business/model")
	public ModelAndView modelDesign(){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("page/business/model-design");
		return modelAndView;
	}

	@GetMapping("/page/business/form/{processDefinitionId}/{state}")
	public void formModel(@PathVariable String processDefinitionId, @PathVariable String state, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// 1. 创建模板解析器 并设置相关属性
		ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
		// 配置解析器模板存储文件夹路径
		resolver.setPrefix("template-model/");
		// 配置解析器模板文件后缀
		resolver.setSuffix(".html");

		// 通过template模板获取html内容
		TemplateEngine templateEngine = new TemplateEngine();
		templateEngine.setTemplateResolver(resolver);

		Context context = new Context();
		context.setVariable("html", processFormService.getById(processDefinitionId).getFormHtml());

		if (state.equals("submit")){
			JSONObject object = new JSONObject();
			object.put("id", processDefinitionId);
			context.setVariable("formValue", object);
			context.setVariable("isSelect", Boolean.FALSE);
		}else {
			context.setVariable("formValue", JSONObject.parseObject(activitiesService.getById(state).getFormJson()));
			context.setVariable("isSelect", Boolean.TRUE);
			context.setVariable("timeLine", activitiesService.getTasks(state));
		}

		// 更新请求的页面的内容为无权限页面
		out.write(templateEngine.process("form-model", context));
		out.close();

		//ModelAndView modelAndView = new ModelAndView();
		//JSONObject object = new JSONObject();
		//object.put("id", processDefinitionId);
		//modelAndView.addObject("formValue", object);
		//modelAndView.setViewName("page/business/form-model");
		//return modelAndView;
	}

}
